/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kn.cstf2.transition.Shared;

import com.opensymphony.xwork2.ActionContext;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.transition.Bean.*;
import kn.cstf2.transition.Server.TransitionServer;
import com.mx.kn.cstf2.dto.UsuarioDto;
import com.mx.kn.cstf2.utils.MailSender2;

/**
 *
 * @author user1
 */
public class TransitionShared {

    TransitionServer transitionServer = new TransitionServer();
    private TransitionBean transitionBean = new TransitionBean();

    public TransitionShared() {
        UsuarioDto usuario = new UsuarioDto();
        ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesion = contexto.getSession();
        usuario = (UsuarioDto) sesion.get("usuarioSession");

        //ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesionKnkey = contexto.getSession();
        transitionBean = (TransitionBean) sesionKnkey.get("Knkey");
    }

    public TransitionShared(String nada) {
    }

    public TransitionBean GetTransitionBeanIdKnkey(TransitionBean transitionBean) {

        // TransitionBean transitionBean = new TransitionBean();
        if (transitionBean != null) {
            transitionBean = transitionServer.obtenerTransitionBeanIdKnkeyByKnkey(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public String GetStatusSelected(Integer knkey) {

        TransitionBean transitionBean = new TransitionBean();
        String status = "";
        if (knkey != null) {
            transitionBean = transitionServer.obtenerStatus(knkey);
            status = transitionBean.getStatus();
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return status;
    }

    public String GetStatusImplementationSelected(Integer knkey) {

        TransitionBean transitionBean = new TransitionBean();
        String status = "";
        if (knkey != null) {
            transitionBean = transitionServer.obtenerStatusImplementation(knkey);
            status = transitionBean.getStatusimple();
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return status;
    }

    public T_clientesDataFromStkBean GetDataFromSTK(TransitionBean transitionBean) {
        T_clientesDataFromStkBean t_clientesDataFromStkBean = new T_clientesDataFromStkBean();
        if (t_clientesDataFromStkBean != null) {
            t_clientesDataFromStkBean = transitionServer.obtenerDataFromTClientesSTK(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return t_clientesDataFromStkBean;
    }

    public AsignIfownerStkBean GetIfOwnerFromSTK(TransitionBean transitionBean) {

        AsignIfownerStkBean asignIfownerStkBean = new AsignIfownerStkBean();
        if (asignIfownerStkBean != null) {
            asignIfownerStkBean = transitionServer.obtenerIfOwnerFromSTK(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return asignIfownerStkBean;
    }

    public TransitionBean GetDataFromTransition(TransitionBean transitionBean) {

        //TransitionBean transitionBean=new TransitionBean();
        if (transitionBean != null) {
            transitionBean = transitionServer.obtenerDataFromTransition(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public TransitionBean GetDataFromTransitionIdKnkey(TransitionBean transitionBean) {

        //TransitionBean transitionBean=new TransitionBean();
        if (transitionBean != null) {
            transitionBean = transitionServer.obtenerDataFromTransitionIdKnkey(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public DocumentsBean GetDocumentName(TransitionBean transitionBean) {

        DocumentsBean documentsBean = new DocumentsBean();
        if (documentsBean != null) {
            documentsBean = transitionServer.obtenerDocumentName(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return documentsBean;
    }

  









  






    public void UpdateDetailsInJoindocumenttransition(String detailsnum, Integer documentid, Integer transitionId) {

        if (transitionId != null) {
            transitionServer.actualizarDetailsEnJoindocumenttransition(detailsnum, documentid, transitionId);
        } else {
            //aqui debere validad si es nulo para ocupar el metodo insert
            throw new CstF2Exception("El Usuario es null");
        }
        //return personalBean;
    }




    public void newTransition(int KnKey, String companyName) {
        transitionServer.newTransition(KnKey);
        sendEmail("" + KnKey, companyName);
    }

    private void sendEmail(String knkeys, String companyName) {
        try {
            MailSender2 mailSender = new MailSender2();
            ArrayList<String> direcciones = transitionServer.getSuperUsers();
            StringBuilder mensaje = new StringBuilder();
            mensaje.append("Transition iniciated KNKEY: ");
            mensaje.append(knkeys);
            mensaje.append(" COMPANYNAME: ");
            mensaje.append(companyName);
            
            System.out.println(mensaje);

            mailSender.sendEmail(direcciones, "Assigned knkey confirmation", mensaje);

        } catch (CstF2Exception ex) {
        }

    }

    public boolean transitionExists(int knkey) {
        return transitionServer.transitionExists(knkey);
    }

    public void updateUploaded(String file, int knkey) {
        transitionServer.updateUploaded(file, knkey);
    }

    public void updateUploaded(String file, int knkey, String value) {
        transitionServer.updateUploaded(file, knkey, value);
    }

    public boolean fileUploaded(String file, int knkey) {
        return transitionServer.fileUploaded(file, knkey);
    }





    public java.util.Date GetTheDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
        java.util.Date date = new java.util.Date();

        //dateFormat.format(datesql);
        System.out.println(dateFormat.format(date));

        return date;
    }

    public String GetTheDateTimeString() {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
        java.util.Date date = new java.util.Date();
        java.sql.Date datesql = new java.sql.Date(date.getDate());
        //dateFormat.format(datesql);
        System.out.println(dateFormat.format(date));
        String fecha = datesql.toGMTString();
        return fecha;
    }


   
}
